
import { isUndef } from './isUndef'
import { castPath } from './castPath'
/**
 * 获取对象属性值，路径不存在时不报错
 * const obj = { a: { aa: { aaa: 1 } } };
   safeGet(obj, 'a.aa.aaa'); // -> 1
   safeGet(obj, ['a', 'aa']); // -> {aaa: 1}
   safeGet(obj, 'a.b'); // -> undefined

   isUndef castPath
 * @param {*} obj 目标对象
 * @param {*} path 属性路径
 * @returns 属性值或 undefined
 */
export function safeGet (obj, path) {
  path = castPath(path, obj);
  var prop;
  prop = path.shift();

  while (!isUndef(prop)) {
    obj = obj[prop];
    if (obj == null) return;
    prop = path.shift();
  }

  return obj;
}